<?php
namespace Fox\Core;

use Fox;

/**
 * Fox\Core\Url
 * URL管理类
 * 
 * @author keluo <keluo@focrs.com>
 * @copyright 2017 Focrs, Co.,Ltd
 * @package Fox
 * @since 2017-9-28
 */
class Url {
    
    // 项目asset目录
    public $asset;
    
    public function __construct() {
        $base = Fox::Instance()->isp == 'aliyun'
            ? substr(Fox::Instance()->app, strlen('/data'.substr($_SERVER['DOCUMENT_ROOT'], 4)))
            : substr(Fox::Instance()->app, strlen(dirname($_SERVER['SCRIPT_FILENAME'])));
        
        $base = trim(str_replace('\\', '/', $base), '/');
        $this->asset = Fox::Request()->base.'/'.(strlen($base) ? $base.'/' : '').'asset';
    }
    
    /**
     * 根据路由生成完整URL
     * @param string $route
     * @param mixed $params url中请求参数
     * @return string
     * @example
     * <pre>
     * $url = Fox::Url()->Create('product/view', ['id' => '1']);
     * $url = Fox::Url()->Create('product', 'type=1');
     */
    public function Create($route = '', $params = null) {
        $route = trim($route, '/');
        if(is_array($params)) $params =  http_build_query($params) ;
        $url = Fox::Request()->base.(strlen($route) ? '/'.$route : '');
        if(!strlen($url)) $url = '/';
        return $url.(strlen($params) ? '?'.$params : '');
    }
    
    /**
     * @todo Url签名
     */
    public function Sign() {
        
    }
}